package com.example.mymvvmdemo.diyview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.example.mymvvmdemo.R;


public class MyCircleView extends View {

    private Paint paint = new Paint();
    private int circleColor;

    public MyCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs);
    }

    public MyCircleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyCircleView);
            circleColor = a.getColor(R.styleable.MyCircleView_circleColor, Color.BLUE); // 默认颜色
            a.recycle();
        }
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();
        int radius = Math.min(width, height) / 2;
        paint.setColor(circleColor);
        canvas.drawCircle(width / 2, height / 2, radius, paint);
    }

    public void setCircleColor(int color) {
        this.circleColor = color;
        invalidate(); // 通知视图重新绘制
    }
}